from django.shortcuts import render, get_object_or_404
from teacher.models import Teacher, TeacherHistory, Rating
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from .forms import EditorForm, CommentForm
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
# from . import func
# import mistune

# Create your views here.

def index(request):
	all_teacher = Teacher.objects.all()
	context = {"tea_t":all_teacher}
	return render(request, "teacher/index.html", context)

@login_required
def look(request, teacher_id):
	look_teacher = get_object_or_404(Teacher, teacher_id=teacher_id)
	context = {"look_teacher":look_teacher}
	context['markdown'] = look_teacher.teacher_intro.html
	# 统计得分
	rating = Rating.objects.filter(be_graded=look_teacher)
	lens = Rating.objects.filter(be_graded=look_teacher).count()
	score = {'iq':0, 'eq':0, 'practice':0}
	for rate in rating:
		score['iq'] += rate.iq
		score['eq'] += rate.eq
		score['practice'] += rate.practice
	if lens != 0:
		score['iq'] = round(score['iq']/lens,1)
		score['eq'] = round(score['eq']/lens,1)
		score['practice'] = round(score['practice']/lens,1)
	# print(score['practice'])
	context['score'] = score
	return render(request, "teacher/look.html", context)

@login_required
def history(request, teacher_id):
	look_teacher = get_object_or_404(Teacher, teacher_id=teacher_id)
	all_history = look_teacher.history.all().order_by('-last_edit_date')
	context = {'look_teacher': look_teacher, 'all_history': all_history}
	return render(request, 'teacher/history.html', context=context)

@login_required
def history_spe(request, teacher_id, serial_number):
	now_history = get_object_or_404(TeacherHistory, id=serial_number)
	#render_md = mistune.create_markdown(plugins=['strikethrough', 'table'])
	markdown_html = now_history.teacher_intro.html
	context = {'now_history': now_history, 'markdown_html': markdown_html, 'teacher_id': teacher_id}
	return render(request, 'teacher/history_spe.html', context=context)

@login_required
def editor(request):
	if request.method == 'POST':
		editor_form = EditorForm()
		new_teacher = Teacher()
		if request.POST["intro"] == None or request.POST["subject"] == None or request.POST["name"] == None:
			return render(request, "teacher/editor.html", context={'editor_form': editor_form, 'errmsg': '内容不得为空！'})
		if request.POST['last_year'] == None or request.POST['host_year'] == None:
			return render(request, "teacher/editor.html", context={'editor_form': editor_form, 'errmsg': '内容不得为空！'})

		new_teacher.teacher_intro = request.POST['intro']
		new_teacher.last_year = request.POST['last_year']
		new_teacher.host_year = request.POST['host_year']
		new_teacher.subject = request.POST['subject']
		new_teacher.name = request.POST["name"]
		new_teacher.last_edit_name = request.user.username

		count = 0
		all_teacher = Teacher.objects.all()

		for i in all_teacher:
			count += 1

		new_teacher.teacher_id = count + 1
		new_teacher.save()

		return HttpResponseRedirect(reverse("teacher:look", kwargs={"teacher_id":new_teacher.teacher_id}))
	else:
		editor_form = EditorForm()
		return render(request, "teacher/editor.html", context={'editor_form':editor_form})

@login_required
def editor_old(request, teacher_id):
	look_teacher = get_object_or_404(Teacher, teacher_id=teacher_id)
	editor_form = EditorForm()
	editor_form.fields['name'].initial = look_teacher.name
	editor_form.fields['last_year'].initial = look_teacher.last_year
	editor_form.fields['host_year'].initial = look_teacher.host_year
	editor_form.fields['intro'].initial = look_teacher.teacher_intro
	editor_form.fields['subject'].initial = look_teacher.subject
	context = {"look_teacher":look_teacher, 'editor_form':editor_form}
	if request.method == 'POST':
		look_teacher = get_object_or_404(Teacher, teacher_id=teacher_id)
		if request.POST["intro"] == None or request.POST["subject"] == None or request.POST["name"] == None:
			context['errmsg'] = '你貌似有地方放空了'
			return render(request, 'teacher/editor_old.html', context=context)
		if request.POST['last_year'] == None or request.POST['host_year'] == None:
			context['errmsg'] = '你貌似有地方放空了'
			return render(request, 'teacher/editor_old.html', context=context)
		
		new_history = TeacherHistory()
		new_history.teacher_intro = look_teacher.teacher_intro
		new_history.last_year = look_teacher.last_year
		new_history.host_year = look_teacher.host_year
		new_history.subject = look_teacher.subject
		new_history.name = look_teacher.name
		new_history.last_edit_name = look_teacher.last_edit_name
		new_history.last_edit_date = look_teacher.last_edit
		new_history.history = look_teacher
		new_history.save()

		look_teacher.teacher_intro = request.POST['intro']
		look_teacher.last_year = request.POST['last_year']
		look_teacher.host_year = request.POST['host_year']
		look_teacher.subject = request.POST['subject']
		look_teacher.name = request.POST["name"]
		look_teacher.last_edit_name = request.user.username
		look_teacher.save()

		return HttpResponseRedirect(reverse("teacher:look", kwargs={"teacher_id":look_teacher.teacher_id}))
	else:
		context['errmsg'] = None
		return render(request, "teacher/editor_old.html", context=context)

# 2025/07/08 评论模块
@login_required
def comment_editor(request, teacher_id):
	look_teacher = get_object_or_404(Teacher, teacher_id=teacher_id)
	comment = Rating.objects.filter(grader_teacher=request.user, be_graded=look_teacher)
	comment_form = CommentForm()
	try:
		comment_form.fields['iq'].initial = comment[0].iq
		comment_form.fields['eq'].initial = comment[0].eq
		comment_form.fields['practice'].initial = comment[0].practice
		comment_form.fields['comment'].initial = comment[0].comment
	except:
		pass
	if request.method == 'POST':
		look_teacher = get_object_or_404(Teacher, teacher_id=teacher_id)
		try:
			comment = Rating.objects.filter(grader_teacher=request.user, be_graded=look_teacher).first()
		except:
			comment = Rating()
		if comment == None:
			comment = Rating()
		
		if request.POST["comment"] == None:
			return render(request, "teacher/comment_editor.html", 
				 {'comment_form':comment_form, 'look_teacher':look_teacher, 'error_message':"貌似有一些内容为空"})
		
		# comment = Rating()
		print(comment)
		comment.iq = request.POST['iq']
		comment.eq = request.POST['eq']
		comment.practice = request.POST['practice']
		comment.comment = request.POST['comment']
		comment.grader_teacher = request.user
		comment.be_graded = get_object_or_404(Teacher, teacher_id=teacher_id)
		comment.save()
	
		return HttpResponseRedirect(reverse("teacher:look", kwargs={"teacher_id":teacher_id}))
	else:
		context = {'comment_form':comment_form, 'look_teacher':look_teacher, 'error_message':None}
		return render(request, "teacher/comment_editor.html", context=context)
	
@login_required
def comment_total(request, teacher_id, page_number):
	look_teacher = get_object_or_404(Teacher, teacher_id=teacher_id)
	comments = Rating.objects.filter(be_graded=look_teacher)
	page_com = Paginator(comments, 10)
	try:
		com_now = page_com.page(page_number)
	except PageNotAnInteger:
		return HttpResponseRedirect(reverse("teacher:comment_total", 
									  kwargs={"teacher_id":teacher_id, "page_number":1}))
	except EmptyPage:
		return HttpResponseRedirect(reverse("teacher:comment_total", 
									  kwargs={"teacher_id":teacher_id, 'page_number':page_com.num_pages}))
	
	context =  {'comment_now':com_now, 'look_teacher':look_teacher}
	context['pre'] = com_now.has_previous()
	context['next'] = com_now.has_next()
	context['total'] = page_com.num_pages
	context['now'] = page_number
	context['num_pre'] = page_number-1
	context['num_next'] = page_number+1
	return render(request, 'teacher/comment_total.html', context=context)

@login_required
def comment(request, teacher_id):
	return HttpResponseRedirect(reverse("teacher:comment_total", kwargs={"teacher_id":teacher_id, "page_number":1}))